<template>
  <div class="tktk_container">
    <div class="tktk_btn"><el-button type="primary" size="mini" @click="handleAdd" icon="el-icon-plus">添加新闻</el-button>
    </div>
    <el-table v-loading="loading" :data="newsList" row-key="id">
      <el-table-column prop="headline" width="180" label="新闻标题" align="center"></el-table-column>
      <el-table-column prop="createByName" label="发布者" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="createTime" label="发布时间" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column label="状态" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.showFlag == 0" type="success">显示</el-tag>
          <el-tag v-else type="danger">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="readCount" label="浏览量" align="center"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="box-center">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="params.pageNum"
        layout="prev, pager, next" :page-size="params.pageSize" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { newsList, deleteNews } from '@/api/tktk/news.js'
export default {
  name: 'newsList',
  data() {
    return {
      total: 0,
      loading: false,
      newsList: [],
      params: {
        pageNum: 1,
        pageSize: 10,
      }
    }
  },
  methods: {
    handleUpdate(id) {
      this.$router.push({ path: '/news/add', query: { id } })
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该新闻, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteNews({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getNewsList()
          }
        })
      })
    },
    handleAdd() {
      this.$router.push({ path: '/news/add' })
    },
    handleCurrentChange(page) {
      this.params.pageNum = page
    },

    getNewsList() {
      this.loading = true
      newsList(this.params).then(res => {
        if (res.code === 200) {
          this.newsList = res.rows || []
          this.total = res.total
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    }
  },

  mounted() {
    this.getNewsList()
  }
}



</script>

<style lang="scss">
.search_box {
  margin-bottom: 20px;
}

.upload-goods {
  .el-upload--picture-card {
    width: 80px;
    height: 80px;
    line-height: 78px;

    i {
      font-size: 20px;
    }
  }

  .el-upload-list__item {
    width: 80px;
    height: 80px;
  }
}
</style>
